using System;
using JNPF.Basics.Models;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using JNPF.Utils;
using System.Transactions;
using NPOI.POIFS.FileSystem;

namespace JNPF.Basics.Logic
{
    public class PointswithdrawalBll
    {
        private PointswithdrawalIService service = new PointswithdrawalService();

        private UserextendBll userextendBll = new UserextendBll();



        /// <summary>
        /// 获取信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        public async Task<PointswithdrawalEntity> GetInfo(int id)
        {
            try
            {
                return await service.GetInfo(id);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns></returns>
        public async Task<PageResult<PointswithdrawalListOutput>> GetPageList(PointswithdrawalListQueryInput input)
        {
            try
            {
                return await service.GetPageList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 创建信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Create(PointswithdrawalEntity entity, UserextendEntity userextendEntity)
        {
            try
            {
                //1.创建提现表
                entity.UserId = UserProvider.Instance.Get().UserId;
                entity.Type = 0;
                entity.Points = entity.Points;
                entity.DeleteMark = 0;
                entity.EnabledMark = 1;
                entity.CreatorTime = DateTime.Now;
                entity.CreatorUserId = UserProvider.Instance.Get().UserId;
                entity.CreatorUser = UserProvider.Instance.Get().UserName;
                entity.DeleteMark = 0;
                //插入一条记录表数据
                PointsdetailsEntity partnerPointsdetails = new PointsdetailsEntity();

                //2.查询当前人的
                partnerPointsdetails.PointsFront = userextendEntity.PointsPendingWithdrawal;
                //userextendEntity.TotalPointsEarned += entity.Points;
                userextendEntity.PointsPendingWithdrawal -= entity.Points;
                userextendEntity.WithdrawPoints += entity.Points;
                partnerPointsdetails.UserId = userextendEntity.UserId;
                partnerPointsdetails.State = -1;
                partnerPointsdetails.Type = -1;
                partnerPointsdetails.CustomerLoanUUID = "";
                partnerPointsdetails.PublicName = "提现";
                partnerPointsdetails.Points = entity.Points;
                partnerPointsdetails.PointsAfter = userextendEntity.PointsPendingWithdrawal;
                partnerPointsdetails.SortCode = 0;
                partnerPointsdetails.EnabledMark = 1;
                partnerPointsdetails.CreatorTime = DateTime.Now;
                partnerPointsdetails.CreatorUserId = UserProvider.Instance.Get().UserId;
                partnerPointsdetails.CreatorUser = UserProvider.Instance.Get().UserName;
                partnerPointsdetails.DeleteMark = 0;


                await service.Create(entity, userextendEntity, partnerPointsdetails);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 更新信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Update(int id, PointswithdrawalEntity entity)
        {
            try
            {
                entity.Id = id;
                entity.LastModifyTime = DateTime.Now;
                entity.LastModifyUserId = UserProvider.Instance.Get().UserId;
                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }




        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task Examine(int id, PointswithdrawalCrInputSh input)
        {
            try
            {
                PointswithdrawalEntity pointswithdrawalEntity = await service.GetInfo(id);

                UserextendEntity userextendEntity = await userextendBll.GetInfoByUserId(pointswithdrawalEntity.UserId);
                pointswithdrawalEntity.Type = input.type;
                pointswithdrawalEntity.Remark = input.remark;
                pointswithdrawalEntity.AttachmentInfo = input.attachmentInfo;

                pointswithdrawalEntity.LastModifyTime = DateTime.Now; //审核时间
                pointswithdrawalEntity.LastModifyUserId = UserProvider.Instance.Get().UserId;

                //提现中的积分更改
                userextendEntity.WithdrawPoints -= pointswithdrawalEntity.Points;

                PointsdetailsEntity partnerPointsdetails = null;
                //审核通过
                if (input.type == 1)
                {
                    userextendEntity.TotalPointsSpent += pointswithdrawalEntity.Points;
                }
                else if (input.type == 2)//审核拒绝
                {

                    //这边再添加一条，积分退回记录
                    partnerPointsdetails = new PointsdetailsEntity();
                    partnerPointsdetails.PointsFront = userextendEntity.PointsPendingWithdrawal;
                    //修改总体数据
                    userextendEntity.PointsPendingWithdrawal += pointswithdrawalEntity.Points;
                    partnerPointsdetails.UserId = userextendEntity.UserId;
                    partnerPointsdetails.State = 1;
                    partnerPointsdetails.Type = 1;
                    partnerPointsdetails.CustomerLoanUUID = "";
                    partnerPointsdetails.PublicName = "提现驳回:" + pointswithdrawalEntity.Remark;
                    partnerPointsdetails.Points = pointswithdrawalEntity.Points;
                    partnerPointsdetails.PointsAfter = userextendEntity.PointsPendingWithdrawal;
                    partnerPointsdetails.SortCode = 0;
                    partnerPointsdetails.EnabledMark = 1;
                    partnerPointsdetails.CreatorTime = DateTime.Now;
                    partnerPointsdetails.CreatorUserId = UserProvider.Instance.Get().UserId;
                    partnerPointsdetails.CreatorUser = UserProvider.Instance.Get().UserName;
                    partnerPointsdetails.DeleteMark = 0;
                }
                await service.Examine(pointswithdrawalEntity, userextendEntity, partnerPointsdetails);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }




        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="entity">实体对象</param>
        /// <returns></returns>
        public async Task<int> Delete(PointswithdrawalEntity entity)
        {
            try
            {
                entity.DeleteMark = 1;
                entity.DeleteTime = DateTime.Now; ;
                entity.DeleteUserId = UserProvider.Instance.Get().UserId;

                return await service.Update(entity);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input">过滤条件</param>
        /// <returns></returns>
        public async Task<List<PointswithdrawalEntity>> GetList(PointswithdrawalListQueryInput input)
        {
            try
            {
                return await service.GetList(input);
            }
            catch (Exception ex)
            {
                throw ExceptionEx.Throw(ex);
            }
        }


    }
}